/**
 * Created by xuan on 15/11/30.
 */
define(function (require) {
    'use strict';
    require('angular-mocks');
    require('static_public/js/angular_directive/pagination.js');
    describe('分页控件', function () {
        var scope, $compile, $document, element;

        beforeEach(module('angular.directive.pagination'));
        beforeEach(inject(function ($rootScope, _$compile_, _$document_) {
            scope = $rootScope;
            $compile = _$compile_;
            $document = _$document_;
            resetArgs();
            element = $compile('<div ui-pagination ' +
            'page-size="size"' +
            'total="total"' +
            'page-index="pageIndex"' +
            'changed="change($pageIndex)"></div>')(scope);
            scope.$digest();
        }));
        function resetArgs() {
            scope.size = 10;
            scope.total = 0;
            scope.pageIndex = 1;
            scope.change = function (index) {
                scope.pageIndex = index;
            };
        }

        function getTotalEl() {
            return element.children().first().find('span');
        }

        function getPagesEl() {
            return element.find('.fr').not('.pagination-con').find('span');
        }

        function getCurrEl() {
            return element.find('.pagination-con').find('.current').not('.prev');
        }

        it('添加样式pagination', function () {
            expect(element.hasClass('pagination')).toBe(true);
        });
        it('change total', function () {
            scope.total = 100;
            scope.$digest();
            expect(getTotalEl().text()).toEqual('100');
            expect(getPagesEl().text()).toEqual('10');
            expect(getCurrEl().text()).toEqual('1');
        });
        it('change total pageIndex', function () {
            scope.total = 200;
            scope.pageIndex = 2;
            scope.$digest();
            expect(getTotalEl().text()).toEqual('200');
            expect(getPagesEl().text()).toEqual('20');
            expect(getCurrEl().text()).toEqual('2');
        });
        it('pageIndex=1时，prev 不能点击', function () {
            scope.total = 200;
            scope.pageIndex = 1;
            scope.$digest();
            expect(element.find('.prev')[0].nodeName).toEqual('SPAN');
        });
        it('pageIndex=last时，next 不能点击', function () {
            scope.total = 200;
            scope.pageIndex = 20;
            scope.$digest();
            expect(element.find('.next')[0].nodeName).toEqual('SPAN');
        });
        it('click prev next', function () {
            scope.total = 200;
            scope.pageIndex = 1;
            scope.$digest();
            expect(scope.pageIndex).toEqual(1);
            element.find('.next').click();
            expect(scope.pageIndex).toEqual(2);
            element.find('.prev').click();
            expect(scope.pageIndex).toEqual(1);
        });
        it('click 1 2 3 ...', function () {
            scope.total = 200;
            scope.$digest();
            expect(scope.pageIndex).toEqual(1);
            element.find('.pagination-con a').first().click();
            expect(scope.pageIndex).toEqual(2);
        });
    });
})